<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\helpers\Json;
use common\models\Process;
use yii\web\UploadedFile;
use backend\models\ProcessForm;
use common\models\Department;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use backend\models\SyncProcess;

/**
 * Process controller
 */
class ProcessController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['create', 'datalist', 'order', 'show', 'getprocess'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /*
    * 新建流程
     */
    public function actionCreate($id=null)
    {
        if(!Yii::$app->user->can('createProcess')){
            throw new ForbiddenHttpException('对不起，您现在还没获得此操作的权限');
        }

        $model = new ProcessForm($id);
        $de_arr = Department::find()->where(['status' => 1])->asArray()->all();
        $process = Process::find()->select('pcode,name')->groupBy('name')->asArray()->all();
        
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->addOrUpdate()) {

                return $this->redirect(['process/datalist']);
            }   
        }

        return $this->render('create', ['model' => $model, 'de_arr' => $de_arr, 'process' => $process]);
    }

    /*
    * 流程排序列表
     */
    public function actionDatalist()
    {        
        if(!Yii::$app->user->can('updateProcess')){
            throw new ForbiddenHttpException('对不起，您现在还没获得此操作的权限');
        }

        $model = Process::find()->orderBy('name,order')->all();
        $depart = Department::find()->asArray()->all();
        $depart_arr = ArrayHelper::map($depart, 'id', 'name');

        return $this->render('datalist', ['model' => $model, 'depart_arr' => $depart_arr]);
    }

    /*
    * 修改排序值
     */
    public function actionOrder()
    {
        $id = trim(Yii::$app->request->get()['id']);
        $val = trim(Yii::$app->request->get()['val']);
        if ($id && $val) {
            $process = Process::findOne($id);
            $process->order = $val;
            if ($process->save(false)) {

                $re_arr = [
                    'flag' => true
                ];

              return JSON::encode($re_arr);
            }
        }
        $re_arr = [
            'flag' => false
        ];

        return JSON::encode($re_arr);
    }

    /**
     * 流程图展示
     */
    public function actionShow()
    {
        $process = Process::find()->groupBy('name')->asArray()->all();
        $process = ArrayHelper::map($process, 'pcode', 'name');

        return $this->render('show', ['process' => $process]);
    }

    /**
     * 异步获取流程信息
     */
    public function actionGetprocess()
    {
        //$pcode = trim(Yii::$app->request->get()['pcode']);
        $model = new SyncProcess;
        if ($model->load(Yii::$app->request->get()) && $model->validate()) {
            if ($model->getProcess()['flag']) {

                return JSON::encode($model->getProcess());
            }
        }

        return JSON::encode([
            'flag' => false
        ]);
    }

}    